/*
 * @Author: yangzonglong
 * @Date: 2021-07-09 13:20:24
 * @version: v1.0.0
 * @Descripttion: table input 控制器
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-07-25 12:21:42
 * @Auditor:
 */
import React, { useCallback } from 'react';
import { Form, Input, } from 'antd';
import update from 'immutability-helper';
import CommonController from '../CommonController';
import InitialValue from './../../components/InitialValue';
import InitialValueEn from './../../components/InitialValueEn';
import { IProps } from '../config';

const TextAreaController = ({
  tableProps,
  required,
  print,
  placeholder,
  placeholderEn,
  initialValue,
  initialValueEn,
  originalIndex,
  columns,
  dataIndex,
  title,
  titleEn,
  width,
  disabled,
  exportFlag,
  changeColumnOptions,
  changeControlProps,
  setCurColumn,
  fontSize,
  fontWeight,
  backgroundColor,
  color,
  customizeStyleFlag
}: IProps) => {
  const onChange = useCallback(
    (value: string) => {
      changeColumnOptions('initialValue', value, originalIndex);
    },
    [changeColumnOptions, originalIndex]
  );

  const onChangeEn = useCallback(
    (value: string) => {
      changeColumnOptions('initialValueEn', value, originalIndex);
    },
    [changeColumnOptions, originalIndex]
  );

  const onChangePlaceholder = useCallback(
    (e) => {
      changeColumnOptions('placeholder', e.target.value, originalIndex);
    },
    [changeColumnOptions, originalIndex]
  );

  const onChangePlaceholderEn = useCallback(
    (e) => {
      changeColumnOptions('placeholderEn', e.target.value, originalIndex);
    },
    [changeColumnOptions, originalIndex]
  );

  const onChangeColumnOptions = useCallback(
    (e: any, k: string, isChecked?: boolean) => {
      changeColumnOptions(k, isChecked ? e.target.checked : e.target.value, originalIndex);
    },
    [changeColumnOptions, originalIndex]
  );

  const _changeControlProps = useCallback(
    (e: any, k: string, isChecked?: boolean) => {
      changeControlProps(
        'tableProps',
        {
          ...tableProps,
          columns: update(columns!, {
            [originalIndex!]: {
              [k]: {
                $set: isChecked ? e.target.checked : e.target.value,
              },
            },
          }),
        },
        true
      );

      setCurColumn?.({ ...columns![originalIndex!], originalIndex, [k]: isChecked ? e.target.checked : e.target.value });
    },
    [changeControlProps, columns, originalIndex, setCurColumn, tableProps]
  );

  return (
    <CommonController
      onChangeControlProps={_changeControlProps}
      onChangeColumnOptions={onChangeColumnOptions}
      {...{ title, titleEn, dataIndex, width, required, print, disabled, exportFlag, fontSize, fontWeight, backgroundColor, color, customizeStyleFlag }}
    >

      <Form.Item label="提示">
        <Input onChange={onChangePlaceholder} value={placeholder} />
      </Form.Item>


      <Form.Item label="英文提示">
        <Input onChange={onChangePlaceholderEn} value={placeholderEn} />
      </Form.Item>

      <InitialValue textArea value={initialValue} onChange={onChange} />
      <InitialValueEn textArea value={initialValueEn} onChange={onChangeEn} />
    </CommonController>
  );
};

export default TextAreaController;
